#include "laurin-utils.h"
#include <netinet/ether.h>
#include <pcap.h>

const char* getReadableIP(u_int32_t ip, bool resolve)
{
	return libnet_addr2name4(ip, (resolve) ? LIBNET_RESOLVE : LIBNET_DONT_RESOLVE);
}

const char* getReadableMAC(u_int8_t mac[ETH_ALEN])
{
	// from netinet/ether.h
	return  ether_ntoa((struct ether_addr*)mac);
}


bool areMACEquals(u_int8_t* op1, u_int8_t* op2)
{
	if(!op1 || !op2 )
		return false;

	for(int i = 0; i < ETH_ALEN; i++)
		if(*(op1 + i) != *(op2 + i))
			return false;

	return true;
}



std::list<Glib::ustring> getAvaibleDevices()
{
	pcap_if_t* devs, *dev;
	char err[PCAP_ERRBUF_SIZE];

	std::list<Glib::ustring> aux;

	if(pcap_findalldevs(&devs, err) == 0)
	{
		for(dev = devs; dev; dev = dev->next )
		{
			aux.push_front(Glib::ustring(dev->name));
		}
	}

	pcap_freealldevs(devs);

	return aux;
}

